package com.brillilab.service.logic.instrument.converter;


import com.brillilab.domain.enums.instrument.AppointmentStateEnum;
import com.brillilab.domain.po.instrument.InstrumentAppointment;
import com.brillilab.domain.vo.instrument.AppointmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * appointment相关对象的转换器
 *
 * @author wmh
 */
public class AppointmentConverter {

    private AppointmentConverter() {}

    /**
     * AppointmentVo转换为Appointment
     * @param appointmentVo
     * @return
     */
    public static InstrumentAppointment appointmentVoToAppointment(AppointmentVo appointmentVo) {
        InstrumentAppointment appointment=new InstrumentAppointment();
        BeanUtils.copyProperties(appointmentVo,appointment);
        Date start=DateConverter.stringToDate(appointmentVo.getStartDate());
        Date end=DateConverter.stringToDate(appointmentVo.getEndDate());
        appointment.setStartTime(start);
        appointment.setEndTime(end);
        return appointment;
    }

    /**
     * Appointment转换为AppointmentVo
     * @param appointment
     * @return
     */
    public static AppointmentVo appointmentToAppointmentVo(InstrumentAppointment appointment){
        AppointmentVo appointmentVo=new AppointmentVo();
        BeanUtils.copyProperties(appointment,appointmentVo);
        appointmentVo.setStartDate(DateConverter.DateToString(appointment.getStartTime()));
        appointmentVo.setEndDate(DateConverter.DateToString(appointment.getEndTime()));
        if(StringUtils.isEmpty(appointment.getComments())){
            appointmentVo.setDetails(" ");
        }else {
            appointmentVo.setText(appointment.getComments() + " - ( 预约人: "+appointment.getUserName()+" )");
            appointmentVo.setDetails(appointment.getComments());
        }
        if(appointment.getState()==null){
            appointment.setState(AppointmentStateEnum.NOT_START.getValue());
        }
        if(appointment.getState().equals(AppointmentStateEnum.END.getValue())){
            appointmentVo.setColor("#D3CECE");
        }
        return appointmentVo;
    }

    /**
     * 批量转换
     * @param appointments
     * @return
     */
    public static List<AppointmentVo> appointmentListToappointmentVoList(List<InstrumentAppointment> appointments){
        if(CollectionUtils.isEmpty(appointments))
            return new ArrayList<>(0);

        List<AppointmentVo> appointmentVos=new ArrayList<>(appointments.size());
        appointments.forEach(e->{
            AppointmentVo appointmentVo=appointmentToAppointmentVo(e);
            appointmentVos.add(appointmentVo);
        });
        return appointmentVos;
    }

}
